using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Plugin.Sns.Scene;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace MicroMsg.UI.Page
{
	public class SnsGroupDetailPage : BasePage
	{
		private enum CurrentOpKind
		{
			Kind_None,
			Kind_Removing,
			Kind_ModifyGroup,
			Kind_ModifyMember,
			Kind_Creating,
			Kind_ModifyGroupAndMember
		}
		private const string TAG = "SnsGroupDetailPage";
		private const int cNMaxGroupName = 19;
		private const int cNPageArgCnt = 2;
		private SnsTagInfo mSnsGroup;
		private List<string> mListNewGroupNames;
		private bool mBIsCreateNew;
		private EventWatcher mWatchSnsGroupOp;
		private object mObjectWatcher = new object();
		private bool mBIsShowingCover;
		private ApplicationBar mBar;
		private ApplicationBarIconButton mBtnDone;
		private ApplicationBarIconButton mBtnDel;
		private bool mBIsModifyingMember;
		private bool mBIsModifyingGroup;
		private bool mBIsRemoving;
		internal Grid LayoutRoot;
		internal PhoneTextBox txtIn;
		internal ModGroupMemberCtrl ctrlMemList;
		private bool _contentLoaded;
		public SnsGroupDetailPage()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Page_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Page_Unloaded));
		}
		private void Page_Loaded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("SnsGroupDetailPage", "Page_Loaded");
		}
		private void Page_Unloaded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("SnsGroupDetailPage", "Page_Unloaded");
		}
		protected override void OnPageIn(object[] object_0)
		{
			Log.smethod_5("SnsGroupDetailPage", "OnPageIn");
			if (object_0 != null)
			{
				if (2 == object_0.Length)
				{
					if (object_0[0] is SnsTagInfo)
					{
						this.mSnsGroup = (object_0[0] as SnsTagInfo);
					}
					else
					{
						if (object_0[0] is List<Contact>)
						{
							List<Contact> list = object_0[0] as List<Contact>;
							if (list == null || list.get_Count() == 0)
							{
								base.GoBack();
								return;
							}
							List<string> list2 = new List<string>();
							using (List<Contact>.Enumerator enumerator = list.GetEnumerator())
							{
								while (enumerator.MoveNext())
								{
									Contact current = enumerator.get_Current();
									if (current == null || string.IsNullOrEmpty(current.strUsrName))
									{
										return;
									}
									list2.Add(current.strUsrName);
								}
							}
							this.mListNewGroupNames = list2;
							this.mBIsCreateNew = true;
						}
						else
						{
							if (object_0[0] is List<string>)
							{
								this.mListNewGroupNames = (object_0[0] as List<string>);
								this.mBIsCreateNew = true;
							}
						}
					}
					string text = object_0[1] as string;
					if (this.mBIsCreateNew && !string.IsNullOrEmpty(text) && SnsGroupMgr.IsNameUseable(text, ""))
					{
						this.txtIn.set_Text(text);
					}
					this.initUI();
					this.setAppBar();
					this.registerEvent();
					this.refreshAppbar();
					return;
				}
			}
		}
		protected override void OnPageOut()
		{
			Log.smethod_5("SnsGroupDetailPage", "OnPageOut");
			this.unRegisterEvent();
		}
		protected override void OnPageBack()
		{
			Log.smethod_5("SnsGroupDetailPage", "OnPageBack");
		}
		private void registerEvent()
		{
			if (this.mWatchSnsGroupOp == null)
			{
				this.mWatchSnsGroupOp = new EventWatcher(this.mObjectWatcher, null, new EventHandlerDelegate(this.OnSnsGroupOp));
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_SNS_TAG_OP_SUCCESS, this.mWatchSnsGroupOp);
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_SNS_TAG_OP_ERR, this.mWatchSnsGroupOp);
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_SNS_TAG_MEMBER_OP_SUCCESS, this.mWatchSnsGroupOp);
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_SNS_TAG_MEMBER_OP_ERR, this.mWatchSnsGroupOp);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchSnsGroupOp != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_SNS_TAG_OP_SUCCESS, this.mWatchSnsGroupOp);
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_SNS_TAG_OP_ERR, this.mWatchSnsGroupOp);
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_SNS_TAG_MEMBER_OP_SUCCESS, this.mWatchSnsGroupOp);
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_SNS_TAG_MEMBER_OP_ERR, this.mWatchSnsGroupOp);
				this.mWatchSnsGroupOp = null;
			}
		}
		private void OnSnsGroupOp(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs != null && evtArgs.mObject != null)
			{
				ulong num = (ulong)evtArgs.mObject;
				if (this.mSnsGroup != null)
				{
					if (num != this.mSnsGroup.nTagID)
					{
						return;
					}
				}
				else
				{
					if (!this.mBIsCreateNew)
					{
						return;
					}
				}
				switch (evtArgs.mEventID)
				{
				case EventConst.ON_NETSCENE_SNS_TAG_OP_SUCCESS:
					if (!this.mBIsModifyingGroup)
					{
						return;
					}
					this.mBIsModifyingGroup = false;
					if (this.mBIsCreateNew)
					{
						this.mBIsModifyingMember = true;
						NetSceneSnsTagMemberOption.Instance.doScene(SnsTagMemOptionOpCode.MM_SNS_TAG_MEM_MOD, num, this.txtIn.get_Text(), this.ctrlMemList.GetNameList());
						return;
					}
					if (!this.mBIsModifyingMember)
					{
						this.releaseCover();
						base.GoBack();
						if (this.mBIsRemoving)
						{
							this.mBIsRemoving = false;
							BasePage.ShowToastInNextTopPage(strings.WCGroup_Delete_Group_Success);
							return;
						}
						BasePage.ShowToastInNextTopPage(strings.WCGroup_Modify_Group_Success);
						return;
					}
					break;
				case EventConst.ON_NETSCENE_SNS_TAG_OP_ERR:
					if (!this.mBIsModifyingGroup)
					{
						return;
					}
					this.mBIsModifyingGroup = false;
					this.mBIsModifyingMember = false;
					this.releaseCover();
					if (this.mBIsRemoving)
					{
						this.mBIsRemoving = false;
						MMMessageBox mMMessageBox = MMMessageBox.Show(strings.WCGroup_Delete_Group_Fail, "", true, strings.COM_Confirm, strings.COM_Cancel);
						mMMessageBox.ReturnEvent = new DlgReturnEvent(this.OnDeleteFailedHandler);
						return;
					}
					if (!this.mBIsModifyingMember)
					{
						MMMessageBox.Show(strings.WCGroup_Modify_Group_Fail, true, "确定");
						return;
					}
					break;
				case EventConst.ON_NETSCENE_SNS_TAG_MEMBER_OP_SUCCESS:
					if (!this.mBIsModifyingMember)
					{
						return;
					}
					this.mBIsModifyingMember = false;
					if (!this.mBIsModifyingGroup)
					{
						this.releaseCover();
						base.GoBack();
						if (this.mBIsCreateNew)
						{
							BasePage.ShowToastInNextTopPage(strings.WCGroup_Create_Group_Success);
							return;
						}
						BasePage.ShowToastInNextTopPage(strings.WCGroup_Modify_Group_Success);
						return;
					}
					break;
				case EventConst.ON_NETSCENE_SNS_TAG_MEMBER_OP_ERR:
					if (!this.mBIsModifyingMember)
					{
						return;
					}
					this.mBIsModifyingMember = false;
					this.mBIsModifyingGroup = false;
					MMMessageBox.Show(strings.WCGroup_Modify_Group_Fail, true, "确定");
					this.releaseCover();
					break;
				default:
					return;
				}
				return;
			}
		}
		private void OnDeleteFailedHandler(DlgReturnEventArg dlgReturnEventArg_0)
		{
			switch (MMMessageBox.GetUserClickResult(dlgReturnEventArg_0))
			{
			case 0:
				base.GoBack();
				break;
			case 1:
				break;
			default:
				return;
			}
		}
		private void txtIn_GotFocus(object sender, RoutedEventArgs e)
		{
			this.disableCtrls();
		}
		private void txtIn_LostFocus(object sender, RoutedEventArgs e)
		{
			if (string.IsNullOrWhiteSpace(this.txtIn.get_Text()))
			{
				MessageBoxManager.ShowToast(strings.WCGroup_Name_Should_Not_Empty, 2500, null, false);
				this.txtIn.Focus();
				return;
			}
			string text = this.txtIn.get_Text().Trim();
			if (text.get_Length() > 19)
			{
				MessageBoxManager.ShowToast(strings.WCGroup_Name_Too_Long, 2500, null, false);
				this.txtIn.Focus();
				return;
			}
			string strOriginUsableName = (this.mSnsGroup == null) ? "" : this.mSnsGroup.strTagName;
			if (!SnsGroupMgr.IsNameUseable(text, strOriginUsableName))
			{
				MessageBoxManager.ShowToast(string.Format(strings.WC_Group_Existed, text), 2500, null, false);
				this.txtIn.Focus();
				return;
			}
			this.enableCtrls();
			this.refreshAppbar();
		}
		private void txtIn_KeyDown(object sender, KeyEventArgs e)
		{
		}
		private void ctrlMemList_mEventContactClicked(object sender, object args)
		{
			if (args == null)
			{
				return;
			}
			Contact contact = args as Contact;
			object[] passData = new object[]
			{
				contact
			};
			SnsUserMainPage.GoToThisPage(passData);
		}
		private void initUI()
		{
			if (this.mSnsGroup == null)
			{
				if (this.mBIsCreateNew)
				{
					if (string.IsNullOrEmpty(this.txtIn.get_Text()))
					{
						string useableGroupName = SnsGroupMgr.GetUseableGroupName();
						this.txtIn.set_Text(useableGroupName);
					}
					this.ctrlMemList.mMemberList = this.mListNewGroupNames;
					this.ctrlMemList.mEventMemberChanged += new MMStandardUIEvent(this.ctrlMemList_mEventMemberChanged);
					this.ctrlMemList.mEventContactClicked += new MMStandardUIEvent(this.ctrlMemList_mEventContactClicked);
				}
				return;
			}
			this.txtIn.set_Text(this.mSnsGroup.strTagName);
			List<string> usersFromGroup = SnsGroupMgr.GetUsersFromGroup(this.mSnsGroup);
			this.ctrlMemList.mMemberList = usersFromGroup;
			this.ctrlMemList.mEventMemberChanged += new MMStandardUIEvent(this.ctrlMemList_mEventMemberChanged);
			this.ctrlMemList.mEventContactClicked += new MMStandardUIEvent(this.ctrlMemList_mEventContactClicked);
		}
		private void releaseCover()
		{
			ProcessingCover.releaseProgressBar(this.LayoutRoot);
			this.mBIsShowingCover = false;
		}
		private void getCover(string strToShow = null)
		{
			ProcessingCover.getProgressBar(this.LayoutRoot, strToShow, null, false, false, null);
			this.mBIsShowingCover = true;
		}
		private void disableCtrls()
		{
			this.ctrlMemList.set_IsEnabled(false);
			if (base.get_ApplicationBar() != null)
			{
				base.get_ApplicationBar().set_IsVisible(false);
			}
		}
		private void enableCtrls()
		{
			this.ctrlMemList.set_IsEnabled(true);
			if (base.get_ApplicationBar() != null)
			{
				base.get_ApplicationBar().set_IsVisible(true);
			}
		}
		private void OnDisposeModifyHandler(DlgReturnEventArg dlgReturnEventArg_0)
		{
			switch (MMMessageBox.GetUserClickResult(dlgReturnEventArg_0))
			{
			case 0:
				base.GoBack();
				break;
			case 1:
				break;
			default:
				return;
			}
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
			if (this.mBIsCreateNew || this.isGroupNameChanged() || this.isNameListChange())
			{
				MMMessageBox mMMessageBox = MMMessageBox.Show("", strings.WCGroup_Ignore_Modify, true, strings.COM_Confirm, strings.COM_Cancel);
				mMMessageBox.ReturnEvent = new DlgReturnEvent(this.OnDisposeModifyHandler);
				cancelEventArgs_0.set_Cancel(true);
			}
		}
		private ApplicationBar setAppBar()
		{
			if (this.mBar != null)
			{
				if (!this.mBar.get_IsVisible())
				{
					this.mBar.set_IsVisible(true);
				}
				return this.mBar;
			}
			this.mBar = new ApplicationBar();
			this.mBar.set_IsMenuEnabled(true);
			this.mBar.set_IsVisible(true);
			ApplicationBarIconButton applicationBarIconButton = new ApplicationBarIconButton(new Uri("/Resource/Img/CommonUse/ico_save.png", 2));
			applicationBarIconButton.set_Text(strings.PCon_Bind_BtnComplete);
			applicationBarIconButton.set_IsEnabled(false);
			applicationBarIconButton.add_Click(new EventHandler(this.btnDone_Click));
			this.mBar.get_Buttons().Add(applicationBarIconButton);
			this.mBtnDone = applicationBarIconButton;
			if (!this.mBIsCreateNew)
			{
				ApplicationBarIconButton applicationBarIconButton2 = new ApplicationBarIconButton(new Uri("/Resource/Img/ContactList/Ico_AppBar_Del.png", 2));
				applicationBarIconButton2.set_Text(strings.WCGroup_Delete_Group);
				applicationBarIconButton2.add_Click(new EventHandler(this.btnDel_Click));
				this.mBar.get_Buttons().Add(applicationBarIconButton2);
				this.mBtnDel = applicationBarIconButton2;
			}
			base.set_ApplicationBar(this.mBar);
			this.setAppBarStyle(base.get_ApplicationBar());
			return this.mBar;
		}
		private void refreshAppbar()
		{
			if (this.mBtnDone == null)
			{
				return;
			}
			if (!this.mBIsCreateNew && !this.isGroupNameChanged() && !this.isNameListChange())
			{
				this.mBtnDone.set_IsEnabled(false);
				return;
			}
			this.mBtnDone.set_IsEnabled(true);
		}
		private void btnDone_Click(object sender, EventArgs e)
		{
			this.getCover(null);
			if (this.mBIsCreateNew)
			{
				this.doCreateNewGroup();
				return;
			}
			if (this.isNameListChange())
			{
				this.doModifyGroup();
			}
			if (this.isGroupNameChanged())
			{
				this.doModifyGroupName();
			}
		}
		private void doCreateNewGroup()
		{
			this.mBIsModifyingGroup = true;
			NetSceneSnsTagOption.Instance.doScene(SnsTagOptionOpCode.MM_SNS_TAG_ADD, 0uL, this.txtIn.get_Text());
		}
		private void doModifyGroupName()
		{
			if (this.mSnsGroup == null)
			{
				return;
			}
			this.mBIsModifyingGroup = true;
			string tagName = this.txtIn.get_Text().Trim();
			NetSceneSnsTagOption.Instance.doScene(SnsTagOptionOpCode.MM_SNS_TAG_MOD, this.mSnsGroup.nTagID, tagName);
		}
		private void doModifyGroup()
		{
			this.mBIsModifyingMember = true;
			NetSceneSnsTagMemberOption.Instance.doScene(SnsTagMemOptionOpCode.MM_SNS_TAG_MEM_MOD, this.mSnsGroup.nTagID, this.mSnsGroup.strTagName, this.ctrlMemList.GetNameList());
		}
		private void btnDel_Click(object sender, EventArgs e)
		{
			if (this.mSnsGroup == null)
			{
				return;
			}
			MMMessageBox mMMessageBox = MMMessageBox.Show("", string.Format(strings.WC_Delete_Group_Confirm, this.mSnsGroup.strTagName), true, strings.COM_Confirm, strings.COM_Cancel);
			mMMessageBox.ReturnEvent = new DlgReturnEvent(this.OnDeleteGroupHandler);
		}
		private void OnDeleteGroupHandler(DlgReturnEventArg dlgReturnEventArg_0)
		{
			switch (MMMessageBox.GetUserClickResult(dlgReturnEventArg_0))
			{
			case 0:
				this.getCover(null);
				this.mBIsModifyingGroup = true;
				this.mBIsRemoving = true;
				NetSceneSnsTagOption.Instance.doScene(SnsTagOptionOpCode.MM_SNS_TAG_DEL, this.mSnsGroup.nTagID, this.mSnsGroup.strTagName);
				break;
			case 1:
				break;
			default:
				return;
			}
		}
		private void ctrlMemList_mEventMemberChanged(object sender, object args)
		{
			this.refreshAppbar();
		}
		private bool isGroupNameChanged()
		{
			return this.mSnsGroup != null && !(this.txtIn.get_Text() == this.mSnsGroup.strTagName);
		}
		private bool isNameListChange()
		{
			List<string> usersFromGroup = SnsGroupMgr.GetUsersFromGroup(this.mSnsGroup);
			List<string> nameList = this.ctrlMemList.GetNameList();
			int num = (usersFromGroup == null) ? 0 : usersFromGroup.get_Count();
			int num2 = (nameList == null) ? 0 : nameList.get_Count();
			if (num != num2)
			{
				return true;
			}
			if (num2 == 0)
			{
				return false;
			}
			bool result;
			using (List<string>.Enumerator enumerator = usersFromGroup.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string current = enumerator.get_Current();
					if (!nameList.Contains(current))
					{
						result = true;
						return result;
					}
				}
				return false;
			}
			return result;
		}
		public static void GotoThisPage(object object_0, string strDefaultName = "")
		{
			object[] object_ = new object[]
			{
				object_0,
				strDefaultName
			};
			ToolFunction.GotoPageForUserControl("/Source/Plugin/Sns/Common/UI/SnsGroup/SnsGroupDetailPage.xaml", object_, false);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Common/UI/SnsGroup/SnsGroupDetailPage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.txtIn = (PhoneTextBox)base.FindName("txtIn");
			this.ctrlMemList = (ModGroupMemberCtrl)base.FindName("ctrlMemList");
		}
	}
}
